package ru.urfc.portal.ui.client;

import ru.urfc.portal.ui.client.controllers.BaseController;
import ru.urfc.portal.ui.client.googlets.Navigator;
import ru.urfc.portal.ui.client.googlets.Sample;
import ru.urfc.portal.ui.client.googlets.TheButton;
import ru.urfc.portal.ui.client.googlets.admin.ClientEdit;
import ru.urfc.portal.ui.client.googlets.admin.ClientInfo;
import ru.urfc.portal.ui.client.googlets.admin.ClientList;
import ru.urfc.portal.ui.client.googlets.admin.ClientNotifications;
import ru.urfc.portal.ui.client.googlets.admin.GlobalNotifications;
import ru.urfc.portal.ui.client.googlets.client.AssociatedUserCreate;
import ru.urfc.portal.ui.client.googlets.client.AssociatedUserEdit;
import ru.urfc.portal.ui.client.googlets.client.AssociatedUserList;
import ru.urfc.portal.ui.client.googlets.client.BalanceOperationList;
import ru.urfc.portal.ui.client.googlets.client.BalanceSource;
import ru.urfc.portal.ui.client.googlets.client.ContragentInfo;
import ru.urfc.portal.ui.client.googlets.client.DeviceInfo;
import ru.urfc.portal.ui.client.googlets.client.DeviceList;
import ru.urfc.portal.ui.client.googlets.client.DeviceStateChangeList;
import ru.urfc.portal.ui.client.googlets.client.DocumentEdit;
import ru.urfc.portal.ui.client.googlets.client.DocumentStateChangeList;
import ru.urfc.portal.ui.client.googlets.client.DocumentsList;
import ru.urfc.portal.ui.client.googlets.client.DocumentsNew;
import ru.urfc.portal.ui.client.googlets.client.InvoiceFactureList;
import ru.urfc.portal.ui.client.googlets.client.InvoiceInfo;
import ru.urfc.portal.ui.client.googlets.client.InvoiceList;
import ru.urfc.portal.ui.client.googlets.client.NetDeviceList;
import ru.urfc.portal.ui.client.googlets.client.NetInfo;
import ru.urfc.portal.ui.client.googlets.client.NetList;
import ru.urfc.portal.ui.client.googlets.client.UserEdit;
import ru.urfc.portal.ui.client.googlets.email.AdminAssociate;
import ru.urfc.portal.ui.client.model.catalogs.CatalogItemWithStyle;
import ru.urfc.portal.ui.client.model.catalogs.DeviceState;
import ru.urfc.portal.ui.client.model.catalogs.DocumentState;
import ru.urfc.portal.ui.client.model.catalogs.InvoiceState;
import ru.urfc.portal.ui.client.model.catalogs.NetLicenseState;
import ru.urfc.portal.ui.client.model.catalogs.NetState;
import ru.urfc.portal.ui.client.model.catalogs.UserState;
import ru.urfc.portal.ui.client.model.catalogs.booleans.ClientAccessEnabled;
import ru.urfc.portal.ui.client.model.catalogs.booleans.ClientAdminRegistered;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class App implements EntryPoint {

	public static final Type<BaseController> DO_UPDATE = new Type<BaseController>();

	private BaseGooglet[] googletsList = {
			new Navigator(),
			new Sample(),
			new ContragentInfo(),
			new ClientList(),
			new ClientInfo(),
			new ClientEdit(),
			new NetList(),
			new NetInfo(),
			new NetDeviceList(),
			new DeviceList(),
			new AssociatedUserList(),
			new AssociatedUserCreate(),
			new AssociatedUserEdit(),
			new DeviceInfo(),
			new DeviceStateChangeList(),
			new InvoiceList(),
			new InvoiceInfo(),
			new InvoiceFactureList(),
			new BalanceOperationList(),
			new BalanceSource(),
			new GlobalNotifications(),
			new ClientNotifications(),
			new UserEdit(),
			new AdminAssociate(),
			new TheButton(),
			new DocumentsList(),
			new DocumentsNew(),
			new DocumentEdit(),
			new DocumentStateChangeList()
	};
	
	static private CatalogItemWithStyle[][] catalogItemsList = {
			DeviceState.values,
			NetState.values,
			NetLicenseState.values,
			DocumentState.values,
			InvoiceState.values,
			UserState.values,
			ClientAdminRegistered.values,
			ClientAccessEnabled.values
	};
	
    @Override
    public void onModuleLoad() {
    	HandlerManager handlerManager = new HandlerManager(null);
    	initEnums();
    	for (BaseGooglet googlet: googletsList) {
    		String id = Utils.getSimpleName(googlet.getClass());
        	addGooglet(handlerManager, id, googlet);
    	}
    	handlerManager.fireEvent(new GwtEvent<BaseController>() {

    		@Override
			public Type<BaseController> getAssociatedType() {
				return DO_UPDATE;
			}

			@Override
			protected void dispatch(BaseController handler) {
				handler.doGet();
			}

    	});
    }

	public static void initEnums() {
		for (CatalogItemWithStyle[] items: catalogItemsList) {
    		EnumRegistry.register(items);
    	}
	}

    /**
	 * Draw widget if find appropriate placeholder
     * @param handlerManager 
	 */
	public void addGooglet(HandlerManager handlerManager, String id, BaseGooglet googlet) {
		if (DOM.getElementById(id) != null) {
			googlet.setName(id);
			googlet.setEventsDispatcher(handlerManager);
			googlet.setUrfcApp(this);
			googlet.init();
			RootPanel.get(id).add(googlet);
		}
	}
	
}
